#include "thresholdmanage.h"
#include "ui_thresholdmanage.h"

ThresholdManage::ThresholdManage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ThresholdManage)
{
    ui->setupUi(this);
    init();
}

ThresholdManage::~ThresholdManage()
{
    delete ui;
}

void ThresholdManage::search()
{
    on_btnSearch_clicked();
}

void ThresholdManage::revert()
{
    on_btnCancel_clicked();
}

void ThresholdManage::submit()
{
    on_btnSubmit_clicked();
}

bool ThresholdManage::editable()
{
    return true;
}

void ThresholdManage::init()
{
    m_editMode = false;

    m_access = new AccessDB(nullptr, "Threshold");
    m_access->initDB();

    this->setAttribute(Qt::WA_StyledBackground);

    initTableView();

}

void ThresholdManage::initTableView()
{
    m_sqlModel = new QSqlTableModel(this, m_access->m_sqlDatabase);

    m_sqlModel->setTable("XXX");

    m_sqlModel->setEditStrategy(QSqlTableModel::OnManualSubmit);

    if (!m_sqlModel->select()) {
        qDebug() << "查询失败";
        qDebug() << m_sqlModel->lastError();
        return;
    }

    ui->tableView->setModel(m_sqlModel);

    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);

    ui->tableView->setShowGrid(false);

    ui->tableView->verticalHeader()->setVisible(false);

    ui->tableView->setEditTriggers(QTableView::NoEditTriggers);
}

bool ThresholdManage::editMode()
{
    return m_editMode;
}


void ThresholdManage::on_btnEdit_clicked()
{
    // TODO
    // 将其余按键状态置灰
    // 将表格的编辑模式打开
    m_editMode = true;

    ui->tableView->setEditTriggers(QAbstractItemView::DoubleClicked);

    ui->btnNew->setDisabled(true);
    ui->btnNew->setDisabled(true);
    ui->btnDelete->setDisabled(true);
    ui->btnCancel->setEnabled(true);
    ui->btnSubmit->setEnabled(true);

}

void ThresholdManage::on_btnNew_clicked()
{
    // TODO
    // 将其余按键状态置灰
    // 将表格的编辑模式打开
    m_editMode = true;

    ui->btnEdit->setDisabled(true);
    ui->btnNew->setDisabled(true);
    ui->btnDelete->setDisabled(true);
    ui->btnCancel->setEnabled(true);
    ui->btnSubmit->setEnabled(true);

    // m_sqlModel每次只能最大缓存256个，所以需要先fetchMore
    while (m_sqlModel->canFetchMore()) {
        m_sqlModel->fetchMore();
    }

    int rowCount = m_sqlModel->rowCount();

    m_sqlModel->insertRow(rowCount);

    m_sqlModel->setData(m_sqlModel->index(rowCount, 0), "");

    ui->tableView->setEditTriggers(QTableView::DoubleClicked);

    ui->tableView->scrollToBottom();

}

void ThresholdManage::on_btnSubmit_clicked()
{
    // TODO
    // 刷新表格的内容
    m_editMode = false;

    int ok = QMessageBox::warning(this, "提交修改", "确定提交修改？", QMessageBox::Yes, QMessageBox::No);

    if (ok == QMessageBox::No) {
        qDebug() << "取消提交";
        return;
    }

    m_sqlModel->submitAll();

    ui->btnNew->setEnabled(true);
    ui->btnEdit->setEnabled(true);
    ui->btnDelete->setEnabled(true);
    ui->btnCancel->setDisabled(true);
    ui->btnSubmit->setDisabled(true);

    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
}

void ThresholdManage::on_btnCancel_clicked()
{
    // TODO
    // 刷新表格的内容
    m_editMode = false;

    m_sqlModel->revertAll();

    ui->btnNew->setEnabled(true);
    ui->btnEdit->setEnabled(true);
    ui->btnDelete->setEnabled(true);
    ui->btnCancel->setDisabled(true);
    ui->btnSubmit->setDisabled(true);

    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
}

void ThresholdManage::on_btnDelete_clicked()
{
    auto index = ui->tableView->currentIndex();
    m_sqlModel->removeRow(index.row());

    int ok = QMessageBox::warning(this, "删除当前行！", "确定删除当前行？", QMessageBox::Yes, QMessageBox::No);

    if (ok == QMessageBox::No) {
        m_sqlModel->revertAll();
        return;
    }

    m_sqlModel->submitAll();
    m_sqlModel->select();
}

void ThresholdManage::on_btnSearch_clicked()
{
    m_sqlModel->setSort(0, Qt::AscendingOrder);

    int result = m_sqlModel->select();
    if (!result) {
        qDebug() << "查询失败: " << m_sqlModel->lastError();
        return;
    }

    ui->btnNew->setEnabled(true);
    ui->btnEdit->setEnabled(true);
    ui->btnDelete->setEnabled(true);
    ui->btnCancel->setDisabled(true);
    ui->btnSubmit->setDisabled(true);

    ui->tableView->scrollToTop();
}
